<template>
    <div class="p-2">
        <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
            :leave-active-class="proxy?.animate.searchAnimate.leave">
            <div class="search">
                <el-form :model="queryParams" ref="queryFormRef" :inline="true">
                    <el-form-item label="申请日期：" prop="timeDate">
                        <el-date-picker v-model="queryParams.timeDate" type="daterange" range-separator="至"
                            start-placeholder="开始时间" end-placeholder="结束时间" @change="handleTimeChange"
                            format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
                    </el-form-item>
                    <el-form-item label="供应商：" prop="orderPrintStatus">
                        <el-select style="width: 120px;" v-model="queryParams.orderPrintStatus" clearable>
                            <el-option label="未打印" value="0"></el-option>
                            <el-option label="已打印" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="审批状态：" prop="orderPrintStatus">
                        <el-select style="width: 120px;" v-model="queryParams.orderPrintStatus" clearable>
                            <el-option label="未打印" value="0"></el-option>
                            <el-option label="已打印" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="模糊查询：" prop="workName">
                        <el-input v-model="queryParams.workName" placeholder="请输入"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </transition>
        <el-card shadow="never">
            <template #header>
                <strong>入库金额：{{ amountOfMoneyTotal }}</strong>
            </template>
            <el-table v-loading="loading" :data="dataList" stripe>
                <el-table-column label="申请日期" prop="createTime" align="center"></el-table-column>
                <el-table-column label="工艺" prop="workName" align="center"></el-table-column>
                <el-table-column label="规格名称" prop="specsDetail" align="center"></el-table-column>
                <el-table-column label="单位" prop="specsUnit" align="center"></el-table-column>
                <el-table-column label="单价" prop="price" align="center"></el-table-column>
                <el-table-column label="供应商" prop="supplyFactoryName" align="center"></el-table-column>
                <el-table-column label="采购数量" prop="purchaseNumber" align="center"></el-table-column>
                <el-table-column label="入库数量" prop="inNumber" align="center"></el-table-column>
                <el-table-column label="入库金额" prop="inAmount" align="center"></el-table-column>
                <el-table-column label="关联订单号" prop="semiProductionCode" align="center"></el-table-column>
                <el-table-column label="备注" prop="remark" align="center"></el-table-column>
                <el-table-column label="对账状态" align="center">
                    <template #default="scope">
                        <span v-if="scope.row.reconciliationFlag === '0'" style="color: #F75C4E;">未对账</span>
                        <span v-if="scope.row.reconciliationFlag === '1'" style="color: #FFA940;">已对账</span>
                        <span v-if="scope.row.reconciliationFlag === '2'" style="color: #52CC6F;">已付款</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" align="center" width="220px">
                    <template #default="scope">
                        <el-tooltip content="对账" placement="top">
                            <el-button @click="handleOpenApply(scope.row)" link :style="{ fontSize: '13px' }"
                                type="success" icon="Money"></el-button>
                        </el-tooltip>
                        <el-tooltip content="撤销对账">
                            <el-button @click="cancellationOfOutsourcing(scope.row, getList)" link
                                :style="{ fontSize: '13px' }" type="danger" icon="RefreshLeft"></el-button>
                        </el-tooltip>
                        <el-tooltip content="删除">
                            <el-button @click="deleteList(scope.row)" link :style="{ fontSize: '15px' }" type="danger"
                                icon="Delete"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>

            </el-table>
            <pagination v-show="totalData > 0" :total="totalData" v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize" @pagination="getList" />
        </el-card>

        <!-- 申请对账 -->
        <ApplyForReconciliation :visible="applyVisible" :initList="applyObj" @submit="getList"
            @close="applyVisible = false" />
    </div>
</template>
<script setup lang="ts">
import { type FormInstance } from 'element-plus';

import { getOuterFactoryMachiningListApi, deleteOuterFactoryMachiningApi } from '@/api/sj/purchaseManagement/outsourcingProcessing/index';

/** 对账 */
import ApplyForReconciliation from './components/applyForReconciliation.vue';
import useApplyForReconciliation from '@/hooks/purchaseManagement/toBeStored/useApplyForReconciliation';
const { applyVisible, applyObj, handleOpenApply, cancellationOfOutsourcing } = useApplyForReconciliation();

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const queryFormRef = ref<FormInstance>();

const dataQueryParams = reactive<pageDataOnlyQuery<any>>({
    queryParams: {
        timeDate: '',
        startDate: '',
        endDate: '',
        pageSize: 10,
        pageNum: 1
    }
});

const { queryParams } = toRefs(dataQueryParams);

const loading = ref(false);
const dataList = ref<any>([]);
const totalData = ref(0);
const amountOfMoneyTotal = ref();

const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
};

const resetQuery = () => {
    queryFormRef.value?.resetFields();
    queryParams.value.startDate = '';
    queryParams.value.endDate = '';
    queryParams.value.timeDate = '';
    handleQuery();
};

/** 日期选择 */
const handleTimeChange = (val: any) => {
    queryParams.value.startDate = val[0];
    queryParams.value.endDate = val[1];
};

/** 列表删除 */
const deleteList = (row: any) => {
    // 材料已入库或者部分入库，您无法制除该亲记录
    ElMessageBox.confirm('确认要将此条信息放入回收站么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    })
        .then(async () => {
            try {
                const { code } = await deleteOuterFactoryMachiningApi(row.id);
                if (code == 200) {
                    ElMessage({
                        type: 'success',
                        message: '删除成功!'
                    });
                    getList();
                }
            } catch (error) {

            }
        })
        .catch(() => { });
};

const getList = async () => {
    loading.value = true
    try {
        const { code, data: { dataPage, inAmount } } = await getOuterFactoryMachiningListApi(queryParams.value);
        if (code == 200) {
            amountOfMoneyTotal.value = Number(inAmount);
            dataList.value = dataPage.records;
            totalData.value = dataPage.total;
            loading.value = false;
        }

    } catch (error) {
        console.log(error);
        loading.value = false;
    }
};

onMounted(() => {
    getList();
});

</script>
<style lang="scss" scoped></style>